<?php
/**
 *
 * @copyright inovia
 *
 */  

class Image {

	protected $path;
	public $image;
	protected $type;
  protected $pathDest;
  protected $pathSource;	



public function __construct($pathSource,$pathDest){
$this->pathDest=$pathDest;
$this->pathSource=$pathSource;
	list($width, $height,$type) = getimagesize($this->pathSource);
	$this->type=$type;

	
	if($type === 1) {
		$type2 = 'gif';
	} 
  elseif($type === 2) {
				$type2 = 'jpeg';
	} 
  elseif($type === 3) {
				$type2 = 'png';
	} 
  else {
  
				throw new Exception('Mauvais type pour l\'image de fond');
	}
	$this->type=$type2;

	$fct = 'imagecreatefrom'. $type2;
	 $this->image = $fct($pathSource);
}


	
	public function setPathDest($pathDest){
	$this->pathDest=$pathdest;
	}
	
	
	public function resize($newWidth,$newHeight){
	$im = imagecreatetruecolor($newWidth, $newHeight);
	
	$width=imagesx($this->image);
	$height=imagesy($this->image);
	//echo $width;
	//echo height;
	imagecopyresampled(
			$im,
			$this->image,
			0, 0,
			0,
			0,
			$newWidth,
			$newHeight,
			$width,
			$height
			);
	$this->image=$im;
	
  }
  
  
  
  public function renderAndSave($type){
  //$this->makeHeaders();
    if ($type=="")   {
      $nameFile=$this->pathDest.".".$this->type;
      $fct="image".$this->type;
    }
    else{
      $nameFile=$this->pathDest.".".$type;
      $fct="image".$type;
    }
    fopen($nameFile,'w');
    $fct($this->image,$nameFile);
  }
  
  
  public function render(){
    if ($type="")   {
      $fct="image".$this->type;
    }
    else{
      $fct="image".$type;
    }
    $fct($this->image);
  }
 
 function copyburn($newWidth,$newHeight,$pct)
{
  $src=$this->image;
  $dst= imagecreate($newWidth, $newHeight);
  
   $w = imagesx($src);
   $h = imagesy($src);
   $i = 0; $j = 0; $k = 0; $rgb = 0;
   $d = array(); $s = array();
  
   for ($i=0; $i<$h; $i++) {
       for ($j=0; $j<$w; $j++) {
           $rgb = imagecolorat($dst,$dstx+$j,$dsty+$i);
           $d[0] = ($rgb >> 16) & 0xFF;
           $d[1] = ($rgb >> 8) & 0xFF;
           $d[2] = $rgb & 0xFF;
          
           $rgb = imagecolorat($src,$j,$i);
           $s[0] = ($rgb >> 16) & 0xFF;
           $s[1] = ($rgb >> 8) & 0xFF;
           $s[2] = $rgb & 0xFF;

           $d[0] -= max($d[0]-$s[0],0)*$pct/100;
           $d[1] -= max($d[1]-$s[1],0)*$pct/100;
           $d[2] -= max($d[2]-$s[2],0)*$pct/100;
              
           imagesetpixel(
               $dst, $dstx+$j, $dsty+$i,
               imagecolorallocate($dst,$d[0],$d[1],$d[2])
           );
       }
   }
}

 
    
    
 public function __destruct(){
   $this->renderAndSave("");
      }
  
  
  private function makeHeaders() {
		header('Expires: Mon, 01 Jan 2000 00:00:00 GMT');
		header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
		header('Cache-Control: no-store, no-cache, must-revalidate');
		header('Cache-Control: post-check=0, pre-check=0', false);
		header('Pragma: no-cache');
		header('Content-Type: image/' . $this->type);
	}
	
}
?>
